package com.project2.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project2.bean.CommonData;
import com.project2.bean.PageBo;
import com.project2.bean.po.MarketAdmin;
import com.project2.bean.po.MarketAdminExample;
import com.project2.bean.po.MarketSystem;
import com.project2.bean.po.MarketSystemExample;
import com.project2.bean.vo.AdminAdminVO;
import com.project2.bean.vo.AdminCreateVo;
import com.project2.mapper.MarketAdminMapper;
import com.project2.mapper.MarketSystemMapper;
import com.project2.util.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author : bunnyupup
 * @description :
 * @date : Created in 2022/8/12 16:10
 */
@Service
public class AdminAdminServiceImpl implements AdminAdminService {
    @Autowired
    MarketAdminMapper marketAdminMapper;

    @Override
    public CommonData<MarketAdmin> list(String username, PageBo info) {
        // 在执行查询之前开启分页
        PageHelper.startPage(info.getPage(), info.getLimit());

        // 根据姓名做一个查询
        MarketAdminExample example = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        // 条件查询，姓名不为空的情况下，才做模糊查询
        if (username != null && !"".equals(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }

        // 设定排序
        example.setOrderByClause(info.getSort() + " " + info.getOrder());

        List<AdminAdminVO> adminAdminVOS = new ArrayList<>();

        List<MarketAdmin> marketAdmins = marketAdminMapper.selectByExample(example);
        for (MarketAdmin marketAdmin : marketAdmins) {
            AdminAdminVO adminAdminVO = new AdminAdminVO();
            adminAdminVO.setId(marketAdmin.getId());
            adminAdminVO.setAvatar(marketAdmin.getAvatar());
            adminAdminVO.setUsername(marketAdmin.getUsername());
            adminAdminVO.setRoleIds(marketAdmin.getRoleIds());
//            AdminAdminVO adminAdminVO = new AdminAdminVO(marketAdmin);
//            adminAdminVO.setPassword(null);
//            adminAdminVO.setAddTime(null);
//            adminAdminVO.setUpdateTime(null);
//            adminAdminVO.setDeleted(null);
            adminAdminVOS.add(adminAdminVO);
        }

        // 分页结果
        PageInfo<AdminAdminVO> pageInfo = new PageInfo<>(adminAdminVOS);

        return CommonData.data(pageInfo);
    }


    //    @Override
//    public int create(AdminCreateVo vo) {
//        MarketAdmin admin = new MarketAdmin();
//        Date date = new Date();
//        admin.setAddTime(date);
//        admin.setUpdateTime(date);
//        admin.setDeleted(false);
//        admin.setPassword(vo.getPassword());
//        int affectRow = marketAdminMapper.insert(admin);
//        return affectRow;
//    }
    @Override
    public int create(MarketAdmin marketAdmin) throws Exception {
        // 先判断数据库中是否有重名管理员
        MarketAdminExample example = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(marketAdmin.getUsername());
        List<MarketAdmin> marketAdmins = marketAdminMapper.selectByExample(example);
        if (marketAdmins.size() != 0) {
            // 说明有重名
            return 0;
        }
        // 补充前端传入admin对象中没有的数据（时间、删除标记）再插入
        Date date = new Date();
        marketAdmin.setAddTime(date);
        marketAdmin.setUpdateTime(date);
        marketAdmin.setDeleted(false);
        // 加密密码后插入数据库
        marketAdmin.setPassword(Md5Util.getMd5(marketAdmin.getPassword(), marketAdmin.getUsername()));
        int affectedRows = marketAdminMapper.insert(marketAdmin);
        return affectedRows;
    }

    @Override
    public int update(MarketAdmin marketAdmin) throws Exception {
        // 先判断数据库中是否有重名管理员
        MarketAdminExample example = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(marketAdmin.getUsername());
        List<MarketAdmin> marketAdmins = marketAdminMapper.selectByExample(example);
        if (marketAdmins.size() != 1) {
            // 说明有重名
            return 0;
        }

        // 加密密码
        marketAdmin.setPassword(Md5Util.getMd5(marketAdmin.getPassword(), marketAdmin.getUsername()));
        // 更新时间
        marketAdmin.setUpdateTime(new Date());
        int affectedRows = marketAdminMapper.updateByPrimaryKeySelective(marketAdmin);
        return affectedRows;
    }

    @Override
    public int delete(MarketAdmin marketAdmin) {
        // 修改删除标记字段
        marketAdmin.setDeleted(true);
        // 修改数据库
        int affectedRows = marketAdminMapper.updateByPrimaryKeySelective(marketAdmin);
        return affectedRows;
    }
}
